import {
  appItem,
  compoItem,
  editParams,
  namespaceItem,
  routeLinkItem,
  tagItem,
  tagParams,
} from './model/tagManagementModel';
import { upgradeHttp } from '/@/utils/http/axios';

enum Api {
  GET_TAG_TABLE = '/v1/componentLink/page',
  GET_NAMESPACE_LIST = '/v1/componentLink/namespaceList',
  GET_APP_LIST = '/v1/componentLink/appList',
  GET_COMPO_LIST = '/v1/componentLink/componentList',
  GET_ROUTE_LINK_LIST = '/v1/componentLink/linkList',
  EDIT_TAG = '/v1/componentLink/edit',
}

//获取规则表格
export const getTagTable = async (params: tagParams) => {
  const res = await upgradeHttp.post<tagItem[]>({
    url: Api.GET_TAG_TABLE,
    params,
  });

  return res;
};
//获取路由链路，所属链路
export const getRouteLink = async () => {
  const res = await upgradeHttp.get<routeLinkItem[]>({
    url: Api.GET_ROUTE_LINK_LIST,
  });
  const result = res.map((item) => {
    return {
      label: item.name,
      value: item.routeLinkKey,
    };
  });
  return result;
};
//获取应用
export const getAppList = async () => {
  const res = await upgradeHttp.get<appItem[]>({ url: Api.GET_APP_LIST });

  const result = res.map((item) => {
    return {
      label: item.appName,
      value: item.appId,
    };
  });
  return result;
};
//获取组件
export const getCompoList = async (params: number[]) => {
  const res = await upgradeHttp.post<compoItem[]>({ url: Api.GET_COMPO_LIST, params });

  const result = res.map((item) => {
    return {
      label: item.name,
      value: item.id,
    };
  });

  return result;
};
//获取命名空间
export const getNamespaceList = async () => {
  const res = await upgradeHttp.get<namespaceItem[]>({ url: Api.GET_NAMESPACE_LIST });

  const result = res.map((item) => {
    return {
      label: item,
      value: item,
    };
  });
  return result;
};
//编辑
export const editTag = async (params: editParams) => {
  const res = await upgradeHttp.put<string>({
    url: Api.EDIT_TAG,
    params,
  });
  return res;
};
